package chapter03

/**
 * @program lu-scala01
 * @description: 构造函数参数
 * @author: zhanglu
 * @create: 2022-03-10 11:47:00
 */
object C06_ConstructorParams {

  def main(args: Array[String]): Unit = {
    /*
      不加val或var：此时该参数只能作为类的不可变参数使用，外部无法访问该变量
      加val或var：当构造器参数前加上var或val时，该参数由不可变参数变成成员变量，外部也可以调用
     */
    val student02 = new Student2
    student02.name = "lu"
    student02.age = 18
    println(s"s2 name ${student02.name} age ${student02.age}")

    val student03 = new Student03("lu", 18)
    println(s"s3 name ${student03.name} age ${student03.age}")
    student03.age = 20
    println(s"s3 name ${student03.name} age ${student03.age}")

    val student04 = new Student04("lu", 18)
//    println(s"s4 name ${student04.name} age ${student04.age}")
    student04.printInfo

    val student05= new Student05("lu", 18)
    println(s"s5 name ${student05.name} age ${student05.age}")
//    student05.age = 20

    var student06= new Student06("lu", 18)
    student06.printInfo
    student06.sex = "男"
    student06.printInfo
    student06= new Student06("lu", 18, "男")
    student06.printInfo

  }

}

class Student2{

  var name: String = _
  var age: Int = _

}

class Student03(var name: String, var age: Int){

}

//主构造器参数无修饰
class Student04(name: String, age: Int){
  def printInfo{
    println(s"s4 name ${name} age ${age}")
  }
}

//主构造器参数无修饰
class Student04_1(_name: String, _age: Int){
  var name: String = _name
  var age: Int = _age
}

class Student05(val name: String, val age: Int){

}

class Student06(var name: String, var age: Int){

  var sex: String = _

  def this(name: String, age: Int, sex: String){
    this(name, age)
    this.sex = sex
  }

  def printInfo{
    println(s"s6 name ${name} age ${age} sex ${sex}")
  }

}